import os

import dashscope
from dashscope import MultiModalConversation
from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
from dashscope import ImageSynthesis
import json

from numba.scripts.generate_lower_listing import description



def transform(file_path,number=1,size='1024*1024'):
    dashscope.api_key = os.getenv("API_KEY")

    # file_path = "G:/音频解析项目/数据集/纯音频数据集/Audio_Song_Actors_01-24/Actor_01/03-02-01-01-01-01-01.wav"  # 替换为你的音频文件路径

    # 请用您的本地音频的绝对路径替换 ABSOLUTE_PATH/welcome.mp3
    audio_file_path = file_path
    messages = [
        {
            "role": "system",
            "content": [{"text": "You are a helpful assistant."}]},
        {
            "role": "user",
            "content": [{"audio": audio_file_path}, {"text": "请用一段话描述音频里面的场景"}],
        }
    ]

    response = MultiModalConversation.call(model="qwen-audio-turbo-latest", messages=messages)
    answer = response["output"]["choices"][0]["message"]["content"][0]["text"]
    description=answer
    print(answer)

    prompt = "以下是一段音频的描述，请根据音频描述生成一幅以音频描述为主体的图片：" + answer

    print('----sync call, please wait a moment----')
    rsp = ImageSynthesis.call(api_key=os.getenv("API_KEY"),
                              model="wanx2.1-t2i-turbo",
                              prompt=prompt,
                              n=number,
                              size=size)
    img_url=rsp["output"]["results"][0]["url"]
    print('response: %s' % rsp)
    if rsp.status_code == HTTPStatus.OK:
        print("")
        # 在当前目录下保存图片
        # for result in rsp.output.results:
        #     file_name = PurePosixPath(unquote(urlparse(result.url).path)).parts[-1]
        #     with open('./%s' % file_name, 'wb+') as f:
        #         f.write(requests.get(result.url).content)
    else:
        print('sync_call Failed, status_code: %s, code: %s, message: %s' %
              (rsp.status_code, rsp.code, rsp.message))
    return img_url, description

if __name__ == '__main__':
    transform("G:/音频解析项目/数据集/纯音频数据集/Audio_Song_Actors_01-24/Actor_01/03-02-01-01-01-01-01.wav")